package com.one.efaimaly.utils;


import android.Manifest;
import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.content.Context;
import android.support.v4.app.FragmentActivity;

import com.one.common.model.http.RxHelper;
import com.one.common.utils.Logger;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;

import io.reactivex.functions.Consumer;

/**
 * 定位、地址操作 相关Utils
 */
public class LocationUtils {
    /**
     * 自定义的定位回调接口
     */
    public interface OnLocationListener {
        void onLocated(TencentLocation aMapLocation);

        void onLocationPermissionFail();
    }

    /**
     * 直接单次定位
     *
     * @param context  使用定位页面的上下文
     * @param listener 定位回调
     */
    @SuppressLint("CheckResult")
    public static void location(final Context context, final OnLocationListener listener) {
        RxPermissions rxPermissions = new RxPermissions((FragmentActivity) context);
        rxPermissions.request(
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION)
                .compose(RxHelper.<Boolean>rxSchedulerHelper()).subscribe
                (new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean){
                        if (aBoolean) {
                            txLocation(context,listener);
                        } else {
                            Logger.i("oye", "定位权限没开");
                            listener.onLocationPermissionFail();
                        }
                    }
                });
    }

    /**
     * 腾讯定位
     */
    private static void txLocation(final Context context, final OnLocationListener listener){
        TencentLocationRequest request = TencentLocationRequest.create();
        request.setInterval(20000)
                .setRequestLevel(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION)
                .setAllowCache(true);
        final TencentLocationManager locationManager = TencentLocationManager.getInstance(context);

        int error = locationManager.requestLocationUpdates(request, new TencentLocationListener() {
            @Override
            public void onLocationChanged(TencentLocation tencentLocation, int i, String s) {
                if (TencentLocation.ERROR_OK == i) {
                    Logger.d("zlj 定位成功 "+tencentLocation.toString()+" \n"+s);
                    listener.onLocated(tencentLocation);
                    // 定位成功之後 关闭定位
                    locationManager.removeUpdates(this);
                } else {
                    Logger.e("zlj 定位失败");
                }
            }
            @Override
            public void onStatusUpdate(String s, int i, String s1) {
            }
        });
        if(error == 0){
            Logger.d("zlj 注册位置监听器成功");
        } else {
            Logger.d("zlj 注册位置监听器失败");
        }
    }

}